<style lang="scss">
.fieldContent {
  .el-collapse-item__header {
    padding: 0 25px;
  }

  .el-collapse-item__arrow {
    font-size: 23px;
    color: #aaa;
  }
}

.fieldsModel {
  .my-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

  .el-dialog__title {
    display: flex;
    align-items: center;
    font-size: 25px;
    color: #aaa;
  }
}
</style>

<style scoped lang="scss">
.field {
  background-color: #f5f5f5;
  height: 100%;
  overflow: hidden;
  user-select: none;
}

.fieldWrapper {
  width: 80%;
  margin: 0 auto;

  header {
    height: 50px;
    font-size: 16px;
    line-height: 50px;
    color: #606060;

    span {
      font-size: 18px;
      font-weight: 500;
      color: black;
    }
  }

  .fieldContent {
    box-sizing: border-box;
    box-shadow: 2px 2px 5px rgb(0, 0, 0, 0.3);
    position: relative;

    .venueName {
      display: flex;
      align-items: center;
      width: 100%;
      position: absolute;
      box-sizing: border-box;

      p {
        display: block;
        width: 30px;
        height: 30px;
        font-size: 12px;
        color: white;
        text-align: center;
        line-height: 30px;
        border-radius: 50%;
        background-color: #ffdb76;
        margin-right: 15px;
      }

      span {
        font-size: 18px;
      }
    }

    .navMain {
      height: 60px;
      margin: 0 20px;
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      border-top: 1px solid #dddddd;

      .navMainLeft {
        margin-left: 60px;
        display: flex;

        .active {
          background-color: #ffdb76;
          color: white;
        }

        span {
          display: inline-block;
          line-height: 50px;
          padding: 0 25px;
          border-radius: 3px;
          font-size: 16px;
          cursor: pointer;

          &:hover {
            background-color: #ffdb76;
            color: white;
          }

        }
      }

      .navMainRight {
        height: 60px;
        display: flex;
        align-items: center;

        span {
          display: inline-block;
          height: 25px;
          line-height: 25px;
          padding: 0 10px;
          background-color: #a5d1f1;
          margin-right: 20px;
          border-radius: 5px;
          font-size: 14px;
          color: white;
          cursor: pointer;
        }
      }
    }

    main {
      box-sizing: border-box;
      margin-top: 10px;
      background-color: white;
      padding: 25px;
      display: flex;
      flex-wrap: wrap;
      overflow-y: auto;
      margin: 0;

      .mainItem {
        box-sizing: border-box;
        width: 29.333333333%;
        border: 1px dashed #dddddd;
        padding: 30px;
        margin: 0 2% 20px 2%;

        p:first-of-type {
          font-size: 20px;
        }

        p {
          margin: 0;
          font-size: 16px;
          line-height: 30px;
        }

        .set {
          margin-top: 20px;
          display: flex;
          justify-content: space-evenly;

          span {
            padding: 5px 20px;
            font-size: 12px;
            color: white;
            border-radius: 5px;
            background-color: #b3e7f0;
            cursor: pointer;
          }
        }

        .el-switch {
          --el-switch-on-color: #dddddd;
          --el-switch-off-color: #fdb869;
          margin-left: 20px;
        }
      }
    }
  }

  .pag {
    width: 100%;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  // 新增与修改弹出框
  .fieldsModel {
    box-sizing: border-box;

    .fieldsModeltitle {
      color: #fec469;
      margin-left: 20px;
    }

    .closeFieldsModel {
      font-size: 22px;
      display: inline-block;
      width: 25px;
      height: 25px;
      border-radius: 50%;
      line-height: 25px;
      text-align: center;
      color: #fec469;
      cursor: pointer;
      border: 2px solid #fec469;
    }

    .foot {
      width: 100%;
      display: flex;
      justify-content: center;
    }
  }
}
</style>

<template>
  <div class="field">
    <div class="fieldWrapper">
      <header><span>场馆维护 /</span> 场地设置</header>
      <div class="demo-collapse fieldContent">
        <el-collapse accordion>
          <el-collapse-item :name="item.id" v-for="item in stadiumList" :key="item.id">
            <template #title>
              <div class="venueName" @click="getFields(item.sports[0]), indexShow = 0">
                <p>桃浦</p>
                <span>{{ item.name }}</span>
              </div>
            </template>
            <div class="navMain">
              <div class="navMainLeft">
                <span v-for="(sport, index) in item.sports" :key="sport.id" @click="getFields(sport,item), indexShow = index"
                  :class="indexShow == index ? 'active' : ''">{{
                    sport.sport_type_codeName
                  }}</span>
              </div>
              <div class="navMainRight">
                <span @click="toAdd()">新建场地</span>
                <span>批量禁用</span>
              </div>
            </div>
            <main>
              <div class="mainItem" v-for="item in fieldsList" :key="item.id">
                <p>{{ item.name }}</p>
                <p>编号：{{ item.id }}</p>
                <p>类型：<span v-text="item.type == 1 ? '会员' : '共享'"></span></p>
                <div>
                  <span>禁用</span>
                  <el-switch :active-value="1" :inactive-value="0" v-model="item.disable" el-switch-on-color="#ffdb76"
                    @click="idDisabled(item)" />
                </div>
                <div class="set">
                  <span @click="toEidt(item)">修改</span>
                  <span @click="toDel(item)">删除</span>
                </div>
              </div>
            </main>
          </el-collapse-item>
        </el-collapse>
      </div>
      <!-- 分页 -->
      <div class="pag">
        <el-pagination background layout="prev, pager, next" @current-change="currentChange"
          :total="stadiumPagination.total" v-model:page-size="stadiumPagination.size" />
      </div>
      <!-- 添加和修改弹出层 -->
      <div class="fieldsModel">
        <el-dialog v-model="AddFieldsShow" :show-close="false" width="25%">
          <template #header="{ close, titleId, titleClass }">
            <div class="my-header">
              <div :id="titleId" :class="titleClass">
                <el-icon>
                  <DocumentAdd />
                </el-icon>
                <span class="fieldsModeltitle">新增/修改场地</span>
              </div>
              <span @click="close" class="closeFieldsModel">×</span>
            </div>
          </template>
          <el-form :inline="true" :model="fieldForm" class="fieldForm" label-width="120px">
            <el-form-item label="场地名称">
              <el-input v-model="fieldForm.name" placeholder="请输入场地名称" />
            </el-form-item>
            <el-form-item label="类型">
              <el-select v-model="fieldForm.type" class="m-2" placeholder="请选择场地类型">
                <el-option value="1">会员</el-option>
                <el-option value="2">共享</el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="禁用">
              <el-switch v-model="fieldForm.disable" el-switch-on-color="#ffdb76" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <div class="sumAdd" v-if="!showFieldsEdit">
              <el-form-item label="批量添加">
              </el-form-item>
              <el-form-item label="数量">
                <el-input v-model="fieldForm.num" placeholder="请输入数量" />
              </el-form-item>
              <el-form-item label="区分尾缀">
                <el-input placeholder="按数量区分" disabled />
              </el-form-item>
            </div>
            <div class="foot">
              <el-button type="primary" @click="editFields" v-if="showFieldsEdit">确认修改</el-button>
              <el-button type="primary" @click="addFields" v-else>确认添加</el-button>
            </div>
          </el-form>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
// 引入获取场馆设置接口 场地设置接口 新建场地 删除场地 修改场地
import { GetFields, GetStadium, AddFields, DelFields, EditFields } from "../../../../api/stadium";
import { ref } from "vue";
// 场地列表数据
const fieldsList = ref("");
// 单击改变颜色
const indexShow = ref(0);
// 场馆列表数据
const stadiumList = ref("");
// 显示添加和修改场弹出窗
const AddFieldsShow = ref(false);
// 添加修改按钮显示
const showFieldsEdit = ref(false);
//发送场馆请求所需要的数据
const stadiumPagination = ref({ size: 10, order: "asc" });
// 添加修改场地需要的数据
const fieldForm = ref({
  id: '',
    type: '',
    name: "",
    sport_id: '',
    disable: 0,
    is_del: 1,
    num: '',
    company_id:'1'
})
// 获取场地数据
const getFields = (sport) => {
  fieldsList.value = "";
  fieldForm.value.sport_id = sport.id;
  fieldForm.value.company_id=sport.company_id;
  if (sport != null) {
    GetFields({ sport_id: sport.id }).then((res) => {
      fieldsList.value = res.data.data.filter(item => item.sport_id == sport.id)
    });
  } else {
    return
  }

};
// 获取场馆数据
const getStadium = () => {
  GetStadium(stadiumPagination.value).then((res) => {
    stadiumList.value = res.data.data;
    stadiumPagination.value = Object.assign(res.data.pagination, {
      size: 10,
      order: "asc",
    });
  });
};
getStadium();

// 分页点击事件
const currentChange = (val) => {
  stadiumPagination.value.current = val;
  getStadium();
};
// 新建场地
const toAdd = () => {
  AddFieldsShow.value = true;
  showFieldsEdit.value = false;
  let sport_id = fieldForm.value.sport_id;
  let company_id=fieldForm.value.company_id;
  fieldForm.value = {
    id: '',
    type: '',
    name: "",
    sport_id: '',
    disable: 0,
    is_del: 1,
    num: '',
    company_id:'1'
  };
  fieldForm.value.sport_id = sport_id;
  fieldForm.value.company_id=company_id;
  console.log( fieldForm.value);
}
// 新建场地方法
const addFields = () => {
console.log(fieldForm.value);
  let codeArr=ref([]);
  let code = -1;
  if (fieldForm.value.num > 1) {
    let name = fieldForm.value.name;
    for (let i = 1; i <= fieldForm.value.num; i++) {
      fieldForm.value.name = name + i;
      AddFields(fieldForm.value).then(res => {
        codeArr.value.push(res.data.code);
      })
    }
    setTimeout(()=>{
      const result =codeArr.value.every(item => item === 0);
      if(result){
        code=0;
      }
    if (code == 0) {
      ElMessage({
        message: "添加成功",
        type: "success",
      });
      AddFieldsShow.value = false;
      GetFields({ sport_id: fieldForm.value.sport_id }).then((res) => {
        fieldsList.value = res.data.data.filter(item => item.sport_id == fieldForm.value.sport_id)
      });

    } else {
      ElMessage({
        message: "添加失败",
        type: "warning",
      });
    }
    },200)
  }
  else if (fieldForm.value.num <= 1) {
    AddFields(fieldForm.value).then(res => {
      if (res.data.code == 0) {
        ElMessage({
          message: "添加成功",
          type: "success",
        });
        AddFieldsShow.value = false;
        GetFields({ sport_id: fieldForm.value.sport_id }).then((res) => {
          fieldsList.value = res.data.data.filter(item => item.sport_id == fieldForm.value.sport_id)
        });
      } else {
        ElMessage({
          message: "添加失败",
          type: "warning",
        });
      }
    })
  }
}
// 修改场地
const toEidt = (item) => {
  fieldForm.value = {
    id: '',
    type: '',
    name: "",
    sport_id: '',
    disable: 0,
    is_del: 1,
    num: '',
    company_id:'1',
  };
  //显示编辑按钮
  showFieldsEdit.value = true;
  // 显示弹框
  AddFieldsShow.value = true;
  // 定义弹框的内容是编辑的item解构
  fieldForm.value = { ...item };
};
// 定义修改场地方法
const editFields = () => {
  EditFields(fieldForm.value)
    .then((res) => {
      if (res.data.code == 0) {
        ElMessage({
          message: "修改成功",
          type: "success",
        });
        AddFieldsShow.value = false;
        showFieldsEdit.value = false;
        GetFields({ sport_id: fieldForm.value.sport_id }).then((res) => {
          fieldsList.value = res.data.data.filter(item => item.sport_id == fieldForm.value.sport_id * 1)
        });
      } else {
        ElMessage({
          message: "修改失败",
          type: "warning",
        });
      }
    })
    .catch((err) => {
      ElMessage.error(err.data.msg);
      AddFieldsShow.value = false;
      showFieldsEdit.value = false;
    });
};
// 删除
const toDel = (item) => {
  ElMessageBox.confirm("删除不可逆，确认删除吗？", "删除", {
    confirmButtonText: "确定删除",
    cancelButtonText: "取消删除",
    type: "warning",
  })
    .then(() => {
      DelFields(item.id)
        .then((res) => {
          if (res.data.code == 0) {
            ElMessage({
              type: "success",
              message: "删除成功",
            });
            GetFields({ sport_id: item.sport_id }).then((res) => {
              fieldsList.value = res.data.data.filter(item1 => item1.sport_id == item.sport_id)
            });
          } else {
            ElMessage({
              type: "warning",
              message: "删除失败",
            });
          }
        })
        .catch((err) => {
          ElMessage.error(err.data.msg);
        });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除成功",
      });
    });
};
// 是否禁用
const idDisabled = item => {
  fieldForm.value={...item};
  let text='';
  let text1='';
 if(item.disable==1){
   text="确认禁用吗？";
   text1="禁用成功";
 }else{
  text="确认解除禁用吗？";
  text1="解除禁用成功";
 }
  ElMessageBox.confirm(text, "是否禁用", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      EditFields(fieldForm.value)
        .then((res) => {
          if (res.data.code == 0) {
            ElMessage({
              type: "success",
              message: text1,
            });
            GetFields({ sport_id: item.sport_id }).then((res) => {
              fieldsList.value = res.data.data.filter(item1 => item1.sport_id == item.sport_id)
            });
          } else {
            ElMessage({
              type: "warning",
              message: text1+"失败",
            });
          }
        })
        .catch((err) => {
          ElMessage.error(err.data.msg);
        });
    })
    .catch(() => {
      if( fieldForm.value.disable==0){
        fieldForm.value.disable=1;
        EditFields(fieldForm.value)
        .then((res) => {
          if (res.data.code == 0) {
            ElMessage({
              type: "success",
              message: "取消成功",
            });
            GetFields({ sport_id: item.sport_id }).then((res) => {
              fieldsList.value = res.data.data.filter(item1 => item1.sport_id == item.sport_id)
            });
          } else {
            ElMessage({
              type: "warning",
              message: text1+"失败",
            });
          }
        })
        .catch((err) => {
          ElMessage.error(err.data.msg);
        });
      }else{
        fieldForm.value.disable=0;
        EditFields(fieldForm.value)
        .then((res) => {
          if (res.data.code == 0) {
            ElMessage({
              type: "success",
              message: "取消成功",
            });
            GetFields({ sport_id: item.sport_id }).then((res) => {
              fieldsList.value = res.data.data.filter(item1 => item1.sport_id == item.sport_id)
            });
          } else {
            ElMessage({
              type: "warning",
              message: text1+"失败",
            });
          }
        })
        .catch((err) => {
          ElMessage.error(err.data.msg);
        });
      }

     
    });
}
</script>
